package com.hua.common;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.hua.common.commonview.TitleBar;

/**
 * Created by huaxiaolin_pc on 2015/12/16.
 */
public class BaseActivity extends FragmentActivity {

    private TitleBar titleBar = null;
    private RelativeLayout rootView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.baseactivitylayout);
        this.titleBar = (TitleBar) this.findViewById(R.id.titlebar);
        this.rootView = (RelativeLayout) this.findViewById(R.id.childlayout);
        this.initTitlebar();
    }

    public void setContentViewItem(int layoutResource) {
        LayoutInflater.from(this).inflate(layoutResource, this.rootView);
    }

    public TitleBar getTitleBar() {
        return titleBar;
    }

    public void setPageTitle(String title){
        this.titleBar.setTitle(title);
    }




    private void initTitlebar() {
        if (Build.VERSION.SDK_INT >= 19) {
            titleBar.setImmersive(true);
        } else {
            titleBar.setImmersive(false);
        }
        titleBar.setBackgroundColor(Color.parseColor("#0085cf"));
        titleBar.setLeftImageResource(R.drawable.title_back_unselected);
        titleBar.setLeftText("返回");
        titleBar.setLeftTextColor(Color.WHITE);
        titleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleBar.setTitle("标题");
        titleBar.setTitleColor(Color.WHITE);
        titleBar.setSubTitleColor(Color.WHITE);
        titleBar.setActionTextColor(Color.WHITE);
//        titleBar.addAction(new TitleBar.ImageAction(R.drawable.collect) {
//            @Override
//            public void performAction(View view) {
//                Toast.makeText(BaseActivity.this, "点击了星星", Toast.LENGTH_SHORT).show();
//            }
//        });
//
//        titleBar.addAction(new TitleBar.TextAction("发布") {
//            @Override
//            public void performAction(View view) {
//                Toast.makeText(BaseActivity.this, "点击了发布", Toast.LENGTH_SHORT).show();
//            }
//        });

        if (hasKitKat() && !hasLollipop()) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        } else if (hasLollipop()) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    private boolean hasKitKat() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    }

    private boolean hasLollipop() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }
}
